home *** CD-ROM | disk | FTP | other *** search
- ' the Person class demonstrates the IComparable interface
-
- Class Person
- Implements IComparable
-
- ' Public fields
- Public FirstName As String
- Public LastName As String
-
- ' A simple constructor
- Sub New(ByVal firstName As String, ByVal lastName As String)
- Me.FirstName = firstName
- Me.LastName = lastName
- End Sub
-
- ' A property that returns the name in the format "Doe, Joe".
- ReadOnly Property ReverseName() As String
- Get
- Return LastName & ", " & FirstName
- End Get
- End Property
-
- ' This procedure add sorting capabilities to the class.
- Private Function CompareTo(ByVal obj As Object) As Integer _
- Implements IComparable.CompareTo
- ' Null objects always come before anything else.
- If obj Is Nothing Then Return 1
-
- ' Cast to a specific Person object, to avoid late binding.
- Dim other As Person = CType(obj, Person)
- ' Use StrComp to simplify case-insensitive comparisons.
- Return StrComp(ReverseName, other.ReverseName, CompareMethod.Text)
- End Function
- End Class
-
- ' this version of the Person class demonstrates the IComparer interface
-
- Class Person2
- ' Public fields
- Public FirstName As String
- Public LastName As String
-
- ' A simple constructor
- Sub New(ByVal firstName As String, ByVal lastName As String)
- Me.FirstName = firstName
- Me.LastName = lastName
- End Sub
-
- ' A property that returns name in the format "Joe Doe".
- ReadOnly Property CompleteName() As String
- Get
- Return FirstName & " " & LastName
- End Get
- End Property
-
- ' A property that returns name in the format "Doe, Joe".
- ReadOnly Property ReverseName() As String
- Get
- Return LastName & ", " & FirstName
- End Get
- End Property
-
- ' Shared methods that return an auxiliary object.
- Shared Function CompareByName() As IComparer
- Return New ComparerByName()
- End Function
-
- Shared Function CompareByReverseName() As IComparer
- Return New ComparerByReverseName()
- End Function
-
- ' First auxiliary class, to sort on CompleteName.
- Class ComparerByName
- Implements IComparer
-
- Function Compare(ByVal o1 As Object, ByVal o2 As Object) _
- As Integer Implements IComparer.Compare
- ' Cast both objects to Person and do the comparison.
- Dim p1 As Person2 = CType(o1, Person2)
- Dim p2 As Person2 = CType(o2, Person2)
- Return StrComp(p1.CompleteName, p2.CompleteName, _
- CompareMethod.Text)
- End Function
- End Class
-
- ' Second auxiliary class, to sort on ReverseName.
- Class ComparerByReverseName
- Implements IComparer
-
- Function Compare(ByVal o1 As Object, ByVal o2 As Object) _
- As Integer Implements IComparer.Compare
- ' Save code by casting to Person objects on the fly.
- Return StrComp(CType(o1, Person2).ReverseName, _
- DirectCast(o2, Person2).ReverseName, CompareMethod.Text)
- End Function
- End Class
- End Class
-
-
- ' The IPluggableAddin and IHostEnvironment interfaces are implemented by the MyAddin class
-
- Interface IPluggableAddin
- Event Connected()
-
- ReadOnly Property Id() As Long
-
- Property State() As Boolean
-
- Function OnConnection(ByVal environment As String) As Boolean
-
- Sub OnDisconnection()
-
- Class PluginClass
- ' an example of a class nested in an interface
- End Class
-
- End Interface
-
- ' Another interface with just one property
- Interface IHostEnvironment
- ReadOnly Property HashCode() As Long
- End Interface
-
-
- ' The MyAddin class implements the IPluggableAddin interface
-
- Public Class MyAddin
- ' You can have two distinct Implements statements, if you prefer so.
- Implements IPluggableAddin, IHostEnvironment
-
- ' this class exposes the IDisposable interface
- Implements IDisposable
-
- ' The following procedure implements two read-only properties
- ' from distinct interfaces.
- ReadOnly Property Id() As Long Implements IPluggableAddin.Id, IHostEnvironment.HashCode
- Get
- ' ...
- End Get
- End Property
-
-
- Private Property State() As Boolean Implements IPluggableAddin.State
- Get
- ' ...
- End Get
- Set(ByVal Value As Boolean)
- ' ...
- End Set
- End Property
-
- Protected Overridable Function OnConnection(ByVal Environment As String) _
- As Boolean Implements IPluggableAddin.OnConnection
- ' ...
- End Function
-
- Private Sub OnDisconnection() Implements IPluggableAddin.OnDisconnection
- ' ...
- End Sub
-
- ' Dispose should always be defined as Overridable.
- Overridable Sub Dispose() Implements IDisposable.Dispose
- Console.WriteLine("Dispose method in MyAddin class")
- End Sub
-
- Event Connected() Implements InterfacesDemo.IPluggableAddin.Connected
- End Class
-
- ' the AnotherAddin class demonstrates that interfaces are inherited in derived classes
-
- ' This class inherits all the interfaces defined in MyAddin class.
- Class AnotherAddin
- Inherits MyAddin
-
- Protected Overrides Function OnConnection(ByVal Environment As String) As Boolean
- ' ...
- End Function
-
- Overrides Sub Dispose()
- ' Clean-up code for the AnotherAddin class
- ' ...(omitted)...
- Console.WriteLine("Dispose method in AnotherAddin class")
-
- ' Complete the clean-up step by calling the base class's Dispose.
- MyBase.Dispose()
- End Sub
-
- End Class
-
- ' This is a simple interface with one function
-
- Interface IGetObjectRef
- Function GetObjectRef() As Object
- End Interface
-
- ' This is a structure that implements an interface
- Structure MyStruct
- Implements IGetObjectRef
-
- Dim x As Integer
-
- Public Function GetObjectRef() As Object Implements InterfacesDemo.IGetObjectRef.GetObjectRef
- Return Me
- End Function
- End Structure
-
- ' A class for doing case-insensitive string comparisons the VB6 way
-
- Class CaseInsensitiveComparerVB6
- Implements IComparer
-
- Public Function Compare(ByVal o1 As Object, ByVal o2 As Object) As Integer Implements System.Collections.IComparer.Compare
- Return StrComp(o1.ToString, o2.ToString, CompareMethod.Binary)
- End Function
- End Class
-
- ' The WordParser class and its nested class demonstrate the IEnumerable and IEnumerator interfaces
-
- Class WordParser
- ' This Implements statement means that this class supports For Each.
- Implements IEnumerable
-
- ' The sentence being parsed
- Public Source As String
-
- ' A simple constructor
- Sub New(ByVal source As String)
- Me.Source = source
- End Sub
-
- ' This function is called when a For Each is encountered.
- ' It must return an object that supports the IEnumerator interface.
- Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
- ' Return an instance of the nested class.
- Return New WordParserEnumerator(Source)
- End Function
-
- ' The nested class that supports IEnumerator.
- Class WordParserEnumerator
- ' This Implements statement ensures that this class can be used
- ' to enumerate items in a For Each.
- Implements IEnumerator
-
- ' The sentence being parsed (passed in the constructor)
- Dim Source As String
- ' This index is used to visit all the characters in the sentence.
- Dim CharIndex As Integer
- ' The current word
- Dim CurrentWord As String
-
- ' The outer class passes the Source string through this constructor.
- Sub New(ByVal Source As String)
- Me.Source = Source
- ' Reset CharIndex.
- Reset()
- End Sub
-
- ' Reset the counter.
- ' Note that Visual Basic.NET does *not* call this method when a For
- ' Each loop starts, so you must call it in this class's constructor.
- Private Sub Reset() Implements IEnumerator.Reset
- CharIndex = 0
- End Sub
-
- ' This property returns the current element.
- Private ReadOnly Property Current() As Object _
- Implements IEnumerator.Current
- Get
- ' When VB.NET calls this property, the MoveNext method
- ' has already found the current word.
- Return CurrentWord
- End Get
- End Property
-
- #Const USE_REGEX = True
-
- #If Not USE_REGEX Then
-
- ' This function advances the pointer and returns True if a new word
- ' has been found, False otherwise.
- Private Function MoveNext() As Boolean Implements IEnumerator.MoveNext
- ' Advance CharIndex if it is currently pointing to a separator.
- Do While CharIndex < Len(Source) And _
- IsSeparator(Mid(Source, CharIndex + 1, 1))
- CharIndex += 1
- Loop
- ' Return False if there are no more characters.
- If CharIndex >= Len(Source) Then Return False
-
- ' Remember current position of index.
- Dim StartIndex As Integer = CharIndex
-
- ' Continue to loop until we find another separator.
- Do While CharIndex < Len(Source) And _
- Not IsSeparator(Mid(Source, CharIndex + 1, 1))
- CharIndex += 1
- Loop
-
- ' We found a new word.
- CurrentWord = Mid(Source, StartIndex + 1, CharIndex - StartIndex)
- ' Return True to signal success.
- Return True
- End Function
-
- ' Auxiliary function that determines whether a char is a separator.
- Private Function IsSeparator(ByVal c As String) As Boolean
- Return InStr(" ,.:;!?", c) > 0
- End Function
- #Else
- ' This regular expression searches for words
- Dim re As New System.Text.RegularExpressions.Regex("\w+")
-
- ' This function advances the pointer and returns True if a new word
- ' has been found, False otherwise.
- Private Function MoveNext() As Boolean Implements IEnumerator.MoveNext
- ' find the next word.
- Dim ma As System.Text.RegularExpressions.Match
- ma = re.Match(Source, CharIndex)
-
- ' Return False if there are no more characters.
- If Not ma.Success Then Return False
-
- ' We found a new word.
- CurrentWord = ma.Value
-
- ' Remember current position of index.
- CharIndex = ma.Index + ma.Length
-
- ' Return True to signal success.
- Return True
- End Function
- #End If
-
- End Class
- End Class
-
- ' an example of a cloneable class
-
- Class Employee
- Implements ICloneable
-
- Public FirstName As String
- Public LastName As String
- Public Boss As Employee
-
- Sub New(ByVal firstName As String, ByVal lastName As String)
- Me.FirstName = firstName
- Me.LastName = lastName
- End Sub
-
- #Const USE_MEMBERWISECLONE = True
-
- ' The only method of the ICloneable interface
- Public Function Clone() As Object Implements ICloneable.Clone
-
- #If USE_MEMBERWISECLONE Then
- ' this is the easy way for doing shallow copying
- Return Me.MemberwiseClone
-
- #Else
-
- ' this block of code shows how to do "manual cloning".
-
- ' Create a new Employee with same property values.
- Dim e As New Employee(FirstName, LastName)
- ' Properties not accepted in the constructors must be copied manually.
- e.Boss = Me.Boss
- Return e
- #End If
-
- End Function
- End Class
-
- ' an improved example of a clonable class
-
- Class Employee2
- Implements ICloneable
-
- Public FirstName As String
- Public LastName As String
- Public Boss As Employee2
-
- Sub New(ByVal firstName As String, ByVal lastName As String)
- Me.FirstName = firstName
- Me.LastName = lastName
- End Sub
-
- ' The only method of the ICloneable interface
- Public Function Clone() As Object Implements ICloneable.Clone
- ' Start creating a shallow copy of this object.
- ' (This copies all non-object properties in one operation)
- Dim e As Employee2 = DirectCast(Me.MemberwiseClone, Employee2)
-
- ' manually copy the Boss property, reusing its Clone method.
- If Not (e.Boss Is Nothing) Then
- e.Boss = DirectCast(Me.Boss.Clone, Employee2)
- End If
- Return e
- End Function
-
- End Class
-
- ' a cloneable class that uses a strongly typed Clone method.
-
- Class Employee3
- Implements ICloneable
-
- Public FirstName As String
- Public LastName As String
- Public Boss As Employee3
-
- Sub New(ByVal firstName As String, ByVal lastName As String)
- Me.FirstName = firstName
- Me.LastName = lastName
- End Sub
-
- ' The only method of the ICloneable interface (private)
- Private Function CloneMe() As Object Implements ICloneable.Clone
- ' reuses the code in the strong-typed Clone method
- Return Clone()
- End Function
-
- Function Clone() As Employee3
- ' Start creating a shallow copy of this object.
- ' (This copies all non-object properties in one operation)
- Clone = CType(Me.MemberwiseClone, Employee3)
- ' manually copy the Boss property, reusing its Clone method.
- If Not (Clone.Boss Is Nothing) Then
- Clone.Boss = Me.Boss.Clone
- End If
- End Function
- End Class
-
- ' this class shows how to support IEnumerable interface
- ' it provides access to all files in a directory tree
-
- Class FileTree
- Implements IEnumerable
-
- ' the search path
- Public ReadOnly DirPath As String
-
- ' the constructor
- Sub New(ByVal DirPath As String)
- Me.DirPath = DirPath
- End Sub
-
- ' return an Enumerable object
- Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
- Return New FileTreeEnumerator(DirPath)
- End Function
-
- ' the IEnumerator private object
- Class FileTreeEnumerator
- Implements IEnumerator
-
- Dim DirPath As String
-
- ' this contains the Enumerator object for the file list in current dir
- Dim FileEnumerator As IEnumerator
- ' these contain the stack of the Enumerator objects
- ' for subdirectories of all pending directories
- Dim DirEnumerators As New Stack()
-
- Sub New(ByVal DirPath As String)
- ' save the directory path
- Me.DirPath = DirPath
- Reset()
- End Sub
-
- Sub Reset() Implements IEnumerator.Reset
- ' the DirectoryInfo object that represents the root object
- Dim di As New System.IO.DirectoryInfo(DirPath)
-
- ' get the Enumerator object for the file list, and reset it
- FileEnumerator = di.GetFiles.GetEnumerator
- FileEnumerator.Reset()
-
- ' get the Enumerator object for subdir list and reset it
- Dim dirEnum As IEnumerator = di.GetDirectories.GetEnumerator
- dirEnum.Reset()
- ' push it onto the stack
- DirEnumerators.Push(dirEnum)
- End Sub
-
- Function MoveNext() As Boolean Implements IEnumerator.MoveNext
- ' Simply delegate to the File Enumerator object
- If FileEnumerator.MoveNext Then
- ' it returned True, so we can exit
- Return True
- End If
-
- ' if there are no files in the current directory, check whether
- ' there is another subdir in the current directory
-
- ' first, get the current directory enumerator
- Dim dirEnum As IEnumerator = CType(DirEnumerators.Peek, IEnumerator)
-
- ' check whether the current subdir enumerator has items
- Do Until dirEnum.MoveNext
- ' there are no more subdirs at this level, so we must pop
- ' another element of the stack
- DirEnumerators.Pop()
-
- If DirEnumerators.Count = 0 Then
- ' Return False if there are no more subdirs to parse
- Return False
- End If
-
- ' get the current enumerator
- dirEnum = CType(DirEnumerators.Peek, IEnumerator)
- Loop
-
- ' we can create a DirectoryInfo
- Dim di As System.IO.DirectoryInfo = CType(dirEnum.Current, System.IO.DirectoryInfo)
-
- ' store the file enumerator and reset it
- FileEnumerator = di.GetFiles.GetEnumerator
- FileEnumerator.Reset()
-
- ' get the Enumerator object for subdir list and reset it
- dirEnum = di.GetDirectories.GetEnumerator
- dirEnum.Reset()
- ' push it onto the stack
- DirEnumerators.Push(dirEnum)
-
- ' recursively call this routine, in order to process the file enumerator
- Return Me.MoveNext
-
- End Function
-
- ' The current property simply delegates to FileEnumerator.Current
- ReadOnly Property Current() As Object Implements IEnumerator.Current
- Get
- Return FileEnumerator.Current
- End Get
- End Property
-
- End Class
- End Class